import { Injectable } from '@nestjs/common';
import * as nodemailer from 'nodemailer';
import { ConfigurationService } from './configuration.service';
@Injectable()
export class MailService {
  private transporter;
  constructor(private readonly configurationService: ConfigurationService) {
    console.log(
      'this.configurationService',
      this.configurationService.smtpHost,
      this.configurationService.smtpPort,
      this.configurationService.smtpUser,
      this.configurationService.smtpPass,
    );
    this.transporter = nodemailer.createTransport({
      // host: this.configurationService.smtpHost,
      service: 'qq',
      port: this.configurationService.smtpPort,
      secureConnection: true,
      auth: {
        user: this.configurationService.smtpUser,
        pass: this.configurationService.smtpPass,
      },
    });
  }
  async sendEmail(to: string, subject, text, html) {
    console.log(3333, this.configurationService.smtpUser, to);
    let mailOptions = {
      from: this.configurationService.smtpUser,
      to, // 收件人
      subject, // 主题
      text, // 纯文本
      html, // html内容
    };
    const info = await this.transporter.sendMail(mailOptions);
    console.log(`发送成功${info.messageId}`);
  }
}
